define(function (require) {
    var Base = require('common/base/object');
    require('cropperPlugin');
    require('artdialog');
    var tpl = require('text!./templates/cropper.html');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            if (typeof URL == "undefined") {
                alert("请换用chrome、firefox或者IE10浏览器!");
                return;
            }
            var files = params.input.files;
            if (files <= 0)return;
            if (!_this.isImageFile(files[0]))return;
            var imageUrl = URL.createObjectURL(files[0]);

            var cropData = '';
            var d = dialog({
                id: "common-cropper",
                zIndex: 100,
                title: "剪切图片",
                content: _this.render(tpl, {url: imageUrl}),
                width: 'auto',
                padding: "10",
                lock: false,
                ok: function () {
                    params.success(_.base64_encode(cropData));
                },
                cancel: function () {
                }
            });
            d.showModal();

            _this.$('.crop-img').cropper({
                aspectRatio: params.ration ? params.ration : 1,
                //preview: _this.$('.cropper-img-preview').selector,
                strict: true,
                crop: function (e) {
                    cropData = [
                        '{"x":' + e.x,
                        '"y":' + e.y,
                        '"height":' + e.height,
                        '"width":' + e.width,
                        '"rotate":' + e.rotate + '}'
                    ].join();
                }
            });
        },
        isImageFile: function (file) {
            if (file.type) {
                return /^image\/\w+$/.test(file.type);
            } else {
                return /\.(jpg|jpeg|png|gif)$/.test(file);
            }
        }
    });
});